package com.dbq.fast.business.product.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dbq.fast.R;
import com.dbq.fast.util.RudenessScreenHelper;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 该布局用于处理界面中所有状态，例如：没有网络，没有数据，数据异常，服务器错误等
 * <p>
 * 使用注意事项：
 * 1. 设置是否显示的回调，根据回调的参数，设置另一个界面是否显示还是隐藏
 * 2. 设置刷新的加载回调，在回调中处理重新界面数据的功能
 */

public class StatusView extends RelativeLayout {

    public static final int SUCCESS = 000;
    public static final int NET_ERROR = 001;
    public static final int NO_ORDER_INFO = 002;
    public static final int NO_COUPONS = 005;
    public static final int NO_UNVALIED_COUPONS = 006;
    public static final int SERVICE_ERROR = 003;
    public static final int NO_USED_COUPONS = 9;

    public static final int PAYING = 10;
    public static final int PAY_SUCCESS = 11;
    public static final int PAY_FAILED = 21;

    public static final int LOADING = 004;
    private OnRefreshListener listener;
    private OnViewVisibleStatusListener listener2;

    View rootView;
    @BindView(R.id.status_iv)
    ImageView picIV;
    @BindView(R.id.bar)
    ProgressBar pb;
    @BindView(R.id.status_tv)
    TextView tipTV;

    @BindView(R.id.pay_status_title_tv)
    TextView payStatus;

    @BindView(R.id.pay_result_hint_tv)
    TextView payResultHint;

    @BindView(R.id.pay_sure)
    TextView paySure;

    private int status;
    private OnPayStatusListener payStatusListener;

    public StatusView(Context context) {
        this(context, null);
    }

    public StatusView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        rootView = LayoutInflater.from(getContext()).inflate(R.layout.view_status, null);
        ButterKnife.bind(this,rootView);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(rootView, lp);

        this.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRefresh();
            }
        });
    }

    public void setStatus(int status) {
        switch (status) {
            case SUCCESS:
                this.setVisibility(GONE);
                if (listener2 != null) {
                    listener2.onVisible(VISIBLE);
                }
                break;
            case NET_ERROR:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));

                setStatusIv(R.drawable.ic_refresh);
                setStatusTv(R.string.load_fail);
                setStatusVisible(VISIBLE);
                setBarVisible(GONE);
                if (listener2 != null) {
                    listener2.onVisible(GONE);
                }
                break;
            case NO_ORDER_INFO:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));
                resizePic(RudenessScreenHelper.pt2px(getContext(), 165),
                        RudenessScreenHelper.pt2px(getContext(), 140),
                        RudenessScreenHelper.pt2px(getContext(), 197));

                setBarVisible(GONE);
                setStatusVisible(VISIBLE);
                setStatusIv(R.drawable.ic_no_data);
                setStatusTv(R.string.no_order_tip);
                if (listener2 != null) {
                    listener2.onVisible(GONE);
                }
                break;
            case SERVICE_ERROR:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));

                setStatusIv(R.drawable.ic_refresh);
                setStatusTv(R.string.load_fail);
                setStatusVisible(VISIBLE);
                setBarVisible(GONE);
                if (listener2 != null) {
                    listener2.onVisible(GONE);
                }
                break;
            case LOADING:
                this.setVisibility(VISIBLE);
                showTipBelow(1, (int) RudenessScreenHelper.pt2px(getContext(), 15));

                setStatusTv(R.string.waiting);
                setBarVisible(VISIBLE);
                setStatusVisible(GONE);
                if (listener2 != null) {
                    listener2.onVisible(GONE);
                }
                break;
            case NO_COUPONS:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));
                resizePic(RudenessScreenHelper.pt2px(getContext(), 165),
                        RudenessScreenHelper.pt2px(getContext(), 140),
                        RudenessScreenHelper.pt2px(getContext(), 197));

                setBarVisible(GONE);
                setStatusVisible(VISIBLE);
                setStatusIv(R.drawable.ic_no_data);
                setStatusTv(""/*R.string.no_coupons_tip*/);
                if (listener2 != null) {
                    listener2.onVisible(GONE);
                }
                break;
            case NO_UNVALIED_COUPONS:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));
                resizePic(RudenessScreenHelper.pt2px(getContext(), 165),
                        RudenessScreenHelper.pt2px(getContext(), 140),
                        RudenessScreenHelper.pt2px(getContext(), 197));

                setBarVisible(GONE);
                setStatusVisible(VISIBLE);
                setStatusIv(R.drawable.ic_no_data);
                setStatusTv(""/*R.string.no_valid_coupons_tip*/);
                if (listener2 != null) {
                    listener2.onVisible(GONE);
                }
                break;

            case NO_USED_COUPONS:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));
                resizePic(RudenessScreenHelper.pt2px(getContext(), 165),
                        RudenessScreenHelper.pt2px(getContext(), 140),
                        RudenessScreenHelper.pt2px(getContext(), 197));

                setBarVisible(GONE);
                setStatusVisible(VISIBLE);
                setStatusIv(R.drawable.ic_no_data);
                setStatusTv(""/*R.string.no_used_coupons_tip*/);
                if (listener2 != null) {
                    listener2.onVisible(GONE);
                }
                break;
            case PAYING:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));
                resizePic(RudenessScreenHelper.pt2px(getContext(), 165),
                        RudenessScreenHelper.pt2px(getContext(), 140),
                        RudenessScreenHelper.pt2px(getContext(), 197));

                setBarVisible(VISIBLE);
                setStatusVisible(GONE);
                setStatusTv("paying"/*R.string.no_used_coupons_tip*/);

                break;
            case PAY_SUCCESS:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));
                resizePic(RudenessScreenHelper.pt2px(getContext(), 165),
                        RudenessScreenHelper.pt2px(getContext(), 140),
                        RudenessScreenHelper.pt2px(getContext(), 197));

                setBarVisible(GONE);
                setStatusVisible(GONE);
                setTipVisible(GONE);
                payStatus.setText(getResources().getString(R.string.pay_success));
                payResultHint.setText("");
                paySure.setVisibility(VISIBLE);
                paySure.setOnClickListener(v -> {
                    if(payStatusListener!=null){
                        payStatusListener.paySuccess();
                    }
                });

                break;
            case PAY_FAILED:
                this.setVisibility(VISIBLE);
                showTipBelow(0, (int) RudenessScreenHelper.pt2px(getContext(), 40));
                resizePic(RudenessScreenHelper.pt2px(getContext(), 165),
                        RudenessScreenHelper.pt2px(getContext(), 140),
                        RudenessScreenHelper.pt2px(getContext(), 197));

                setBarVisible(GONE);
                setStatusVisible(GONE);
                setTipVisible(GONE);
                payStatus.setText(getResources().getString(R.string.pay_failed));
                payResultHint.setText("");
                paySure.setVisibility(VISIBLE);
                paySure.setOnClickListener(v -> {
                    if(payStatusListener!=null){
                        payStatusListener.payFailed();
                    }
                });
                break;
        }
    }

    /**
     * 重新调整图片大小
     *
     * @param width
     * @param height
     */
    private void resizePic(float width, float height, float distance) {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.width = (int) width;
        lp.height = (int) height;
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        lp.setMargins(0, (int) distance, 0, 0);
        picIV.setLayoutParams(lp);
    }

    /**
     * @param which 0:tip显示在ImageView下方， 1: tip显示在ProgressBar下方
     */
    private void showTipBelow(int which, int distance) {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        if (which == 0) {
            lp.addRule(RelativeLayout.BELOW, R.id.status_iv);
        } else {
            lp.addRule(RelativeLayout.BELOW, R.id.bar);
        }
        lp.setMargins(0, distance, 0, 0);
        tipTV.setLayoutParams(lp);
    }

    private void setStatusIv(int image) {
        picIV.setBackgroundResource(image);
    }

    private void setStatusTv(int strId) {
        setStatusTv(getResources().getString(strId));
    }

    private void setStatusTv(String str) {
        tipTV.setText(str);
    }


    public void setTipVisible(int visible){
        tipTV.setVisibility(visible);
    }
    //设置loading相关的文案的显示
    private void setBarVisible(int visible) {
        pb.setVisibility(visible);
    }

    //设置整体流程相关的显示
    private void setStatusVisible(int visible) {
        picIV.setVisibility(visible);
    }


    public interface OnRefreshListener {
        void onRefresh();
    }

    public void setRefreshListener(OnRefreshListener listener) {
        this.listener = listener;
    }

    public interface OnViewVisibleStatusListener {
        void onVisible(int visible);
    }

    public void setViewVisibleListener(OnViewVisibleStatusListener listener) {
        this.listener2 = listener;
    }

    public void setOnPayStatusListener(OnPayStatusListener payStatusListener){
        this.payStatusListener =payStatusListener;
    }
    public interface OnPayStatusListener{
        void paySuccess();
        void payFailed();
    }
}
